import { createStore } from '@/airstore';
import ApiClient from '@/services/apiClient';
import { formatDate } from '@/helpers/dateutil';

function transformLmoBacklog(trd) {
  return {
    workNo: trd.LMO_Num,
    acno: trd.LMO_AcNo,
    faultdesc: trd.LMO_FaultDesc,
    submitDate: formatDate(trd.LMO_CreateDate, 'YYYY-MM-DD HH:mm'),
    submitter: trd.LMO_CreateUser,
    lmoStatus: trd.LMO_Status,
    apvStatusDes: staDetail(trd.LMO_Status),
    id: trd.Tid,
    sta: trd.LMO_STA,
    ata: [trd.LMO_ATA.slice(0, 2), trd.LMO_ATA.slice(2)],
    rii: trd.LMO_Rii,
    engine: trd.LMO_EngineRun,
    taskStageTid: trd.MpDp_TaskStageTid,
    flightTid: trd.MpDp_FlightTid,
    measure: trd.LMO_Measure,
  };
}
function transformNrcBacklog(trd) {
  return {
    id: trd.Tid,
    workNo: trd.NRC_NO,
    acno: trd.NRC_Acno,
    acdMs: trd.NRCD_AcdMS,
    cardNo: trd.NRC_CardNo,
    ata: [trd.NRC_ATA.slice(0, 2), trd.NRC_ATA.slice(2)],
    level: trd.NRCD_Level,
    orderNO: trd.NRC_OrderNO,
    itemNo: trd.NRCD_R1,
    sta: trd.NRC_STA,
    submitter: trd.NRC_WriteBy,
    submitDate: formatDate(trd.NRC_Date, 'YYYY-MM-DD HH:mm'),
    rii: trd.NRC_RII,
    engine: trd.NRC_RunEng,
    faultdesc: trd.NRC_FaultDescription,
    taskStageTid: trd.NRC_STA2,
    flightTid: trd.NRCD_R5,
    apvStatusDes: staDetail(trd.NRC_CardStatus),
  };
}

function staDetail(stat) {
  if (stat === 1) {
    return '待提交';
  } else if (stat === 2) {
    return '待审核';
  } else if (stat === 3) {
    return '已批准';
  } else if (stat === 4) {
    return '已退回/待提交';
  } else {
    return '';
  }
}

function tranLmoPageToDb(trd) {
  return {
    MpDp_TaskStageTid: trd.taskStageTid,
    MpDp_FlightTid: trd.flightTid,
    LMO_AcNo: trd.acno,
    LMO_ATA: trd.ata.join(''),
    LMO_CreateDate: trd.submitDate,
    LMO_STA: trd.sta,
    LMO_CreateUser: trd.submitter,
    LMO_Rii: trd.rii ? 1 : 0,
    LMO_EngineRun: trd.engine ? 1 : 0,
    LMO_FaultDesc: trd.faultdesc,
    LMO_Measure: trd.measure,
  };
}

function tranNrcPageToDb(trd) {
  return {
    NRC_STA2: trd.taskStageTid,
    NRCD_R5: trd.flightTid,
    NRC_Acno: trd.acno,
    NRCD_AcdMS: trd.acdMs,
    NRC_CardNo: trd.cardNo,
    NRC_ATA: trd.ata.join(''),
    NRCD_Level: trd.level,
    NRC_OrderNO: trd.orderNO,
    NRCD_R1: trd.itemNo,
    NRC_STA: trd.sta,
    NRC_WriteBy: trd.submitter,
    NRC_Date: trd.submitDate,
    NRC_RII: trd.rii ? 1 : 0,
    NRC_RunEng: trd.engine ? 1 : 0,
    NRC_FaultDescription: trd.faultdesc,
  };
}

export default createStore({
  namespace: 'LmoNrcStore',
  lmoBacklogs: [[], []], // lmo 我发起 与 待我审批 两数组对象
  nrcBacklogs: [[], []],
  // 提交lmo审批
  async submitLmoAudit(loginName, lmoData) {
    const srRes = await ApiClient.post('lmoController/v1/subLmo', {
      loginUser: loginName,
      jsonResult: JSON.stringify(tranLmoPageToDb(lmoData)),
    });
    return srRes;
  },
  // 提交Nrc审批
  async submitNrcAudit(loginName, nrcData) {
    const srRes = await ApiClient.post('nrcController/v1/subNrc', {
      loginUser: loginName,
      jsonResult: JSON.stringify(tranNrcPageToDb(nrcData)),
    });
    return srRes;
  },
  setLmoBacklogs(backlogList) {
    this.lmoBacklogs = backlogList.map((bl) => bl.map(transformLmoBacklog));
  },
  // 获取Lmo待办事项列表
  async getLmoBacklogs(loginName) {
    const srRes = await ApiClient.post('lmoController/v1/getLmoApvList', {
      loginUser: loginName,
    });
    if (!srRes.errorCode) {
      this.setLmoBacklogs(srRes.data);
    }
    return srRes;
  },
  // lmo审批同意
  async acceptLmo(loginName, lmoData) {
    const srRes = await ApiClient.post('lmoController/v1/apvLmo', {
      loginUser: loginName,
      jsonResult: JSON.stringify({ Tid: lmoData.id }),
    });
    return srRes;
  },
  // lmo审批退回
  async rejectLmo(loginName, lmoData) {
    const srRes = await ApiClient.post('lmoController/v1/backLmo', {
      loginUser: loginName,
      jsonResult: JSON.stringify({ Tid: lmoData.id }),
    });
    return srRes;
  },
  setNrcBacklogs(backlogList) {
    this.nrcBacklogs = backlogList.map((bl) => bl.map(transformNrcBacklog));
  },
  // 获取Nrc待办事项列表
  async getNrcBacklogs(loginName) {
    const srRes = await ApiClient.post('nrcController/v1/getNrcApvList', {
      loginUser: loginName,
    });
    if (!srRes.errorCode) {
      this.setNrcBacklogs(srRes.data);
    }
    return srRes;
  },
  // Nrc审批同意
  async acceptNrc(loginName, nrcData) {
    const srRes = await ApiClient.post('nrcController/v1/apvNrc', {
      loginUser: loginName,
      jsonResult: JSON.stringify({ Tid: nrcData.id }),
    });
    return srRes;
  },
  // Nrc审批退回
  async rejectNrc(loginName, nrcData) {
    const srRes = await ApiClient.post('nrcController/v1/backNrc', {
      loginUser: loginName,
      jsonResult: JSON.stringify({ Tid: nrcData.id }),
    });
    return srRes;
  },
});
